﻿# -*- coding:utf-8 -*-
import datetime
from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.template import RequestContext, Context
from django.core.urlresolvers import reverse

from ktit.storage.forms import CategoryForm, PartForm, DocumentForm, DocumentReceiptForm, DocumentDeliveryForm 
from ktit.storage.models import Category, Part, PartItem, Document, DocumentItem, DocumentReceipt, DocumentDelivery, Unit, Place
from ktit.storage.categories import create_cat_tree
#from ktit.storage.historypath import *

from ktit.storage.contextprocessors import category_proc

from django.forms.models import inlineformset_factory
#from django.db.aggregates import Sum

from django.utils._threading_local import local



HP_HOME = 0
HP_NEW_CATEGORY = 1
HP_NEW_PART = 2
HP_EDIT_CATEGORY = 3
HP_EDIT_PART = 4

HISTORYPATH = [
    {"id" : 0, "url" : "", "caption" : "Главная", },
    {"id" : 1, "url" : "", "caption" : "Новая категория", },
    {"id" : 2, "url" : "", "caption" : "Новая запчасть", },
    {"id" : 3, "url" : "", "caption" : "Изменение категории", },
    {"id" : 4, "url" : "", "caption" : "Изменение запчасти", },
]



def history_path_add(request, history_id):
    if request.session.has_key('history_path'):
        hp = request.session["history_path"]
        if history_id in hp:
            request.session["history_path"] = hp[:(hp.index(history_id)+1)]
        else:
            request.session["history_path"].append(history_id)
        return 1
    else:
        request.session["history_path"] = [HP_HOME, history_id]


def history_path_clear(request):
    request.session["history_path"] = [HP_HOME]



@login_required
def index(request):
    request.session["current_category"] = None
    history_path_clear(request)
    
    context = Context({
      'current_menu_item' : '1',
      'current_sub_menu_item':'1',
    })    
    return render_to_response('main.html', context, context_instance=RequestContext(request, processors=[category_proc]))

 

